package com.my.json.sensitive.config;

import com.my.json.sensitive.config.handle.ChineseNameSensitiveHandler;
import com.my.json.sensitive.config.handle.FixedPhoneSensitiveHandler;
import com.my.json.sensitive.config.handle.IdCardSensitiveHandler;
import com.my.json.sensitive.config.handle.MobilePhoneSensitiveHandler;
import com.my.json.sensitive.enums.SensitiveType;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author hexinglin
 * @created 2021-12-29 15:27
 **/
public class CompositeSensitiveHandler  implements SensitiveHandler {
    private static List<SensitiveHandler> SENSITIVE_HANDLER_LIST = new ArrayList();

    public CompositeSensitiveHandler() {
    }

    @Override
    public String sensitive(String text, SensitiveType type) {
        Iterator iterator = SENSITIVE_HANDLER_LIST.iterator();

        String result;
        do {
            if (!iterator.hasNext()) {
                return text;
            }

            SensitiveHandler handler = (SensitiveHandler)iterator.next();
            result = handler.sensitive(text, type);
        } while(result == null);

        return result;
    }

    static {
        SENSITIVE_HANDLER_LIST.add(new IdCardSensitiveHandler());
        SENSITIVE_HANDLER_LIST.add(new ChineseNameSensitiveHandler());
        SENSITIVE_HANDLER_LIST.add(new MobilePhoneSensitiveHandler());
        SENSITIVE_HANDLER_LIST.add(new FixedPhoneSensitiveHandler());
    }
}
